﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yottamud_2013_v3_Main;
using Yottamud_2013_v3_Main.Game_Files;
using System.Windows.Controls.Primitives;
using System.Timers;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;
using System.Drawing;

namespace Yottamud_Project_V3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static Yottamud.Terminal gameDisplay = null;
        private static GameResources resources;
        private static SystemSettingsWindow _settingsWindow = null;
        private static PlayerManager _playerManager = null;
        public static string CurrentCommand = string.Empty;

        private UIUpdater _updater;
        private GameDataMonitor gameMonitor;

        public static Yottamud.Terminal SelectedDisplay
        {
            get { return gameDisplay; }
            set { gameDisplay = value; }
        }

        public static SystemSettings.SystemSettingType CurrentSettings
        { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            SetupGameDisplay();
          
            _playerManager = new PlayerManager();

            _updater = new UIUpdater(this);
          
        }

        private void SetupGameDisplay()
        {
            CDisplayPanelBuffer panel = new CDisplayPanelBuffer();

            panel.Location = new System.Drawing.Point(0, 0);
            panel.Dock = System.Windows.Forms.DockStyle.Top | System.Windows.Forms.DockStyle.Left;

            wfhDisplay.Child = new System.Windows.Forms.Control();
            wfhDisplay.Child.Controls.Add(panel);

            gameDisplay = new Yottamud.Terminal(panel);
            gameDisplay.SendBashPrompt = true;
        }

        private void ConnectToServer(string address, int port)
        {
            gameDisplay.Connect(address, port);
        }

        private void SettingMode(object sender, RoutedEventArgs e)
        {
            if (_settingsWindow == null)
            {
                _settingsWindow = new SystemSettingsWindow();
            }

            _settingsWindow.Show();

        }

        private void btnConnectToServer_Click(object sender, RoutedEventArgs e)
        {

            if (CurrentSettings.BBSName != null)
            {
                gameMonitor = new GameDataMonitor(this, gameDisplay);

                ConnectToServer(CurrentSettings.BBSAddress, CurrentSettings.BBSPort);
            }
            else
            {
                MessageBox.Show("Please load a profile");
            }
        }

        public static bool LoadResources()
        {
            if (CurrentSettings.DatabaseFileName != null && CurrentSettings.DatabaseFileName.Length > 0)
            {
                resources = new GameResources(CurrentSettings.DatabaseFileName);
                _playerManager = new PlayerManager();
                _playerManager.PlayerAutoSetting = CurrentSettings.PlayerAutoSettings;
                return true;
            }
            else
            {
                return false;
            }
        }

        private void Show_PopupMonsterToolTip(object sender, MouseEventArgs e)
        {
            ListViewItem item = e.Source as ListViewItem;

            UIUpdater.NpcEntry npcEnt = (UIUpdater.NpcEntry)item.Content;

            ResourceTypes.NpcType npcData = GameResources.FindNPC(npcEnt.NpcNumber);

            if (npcData != null)
            {
                NpcName.Text = "Name: " + npcData.Name;
                NpcHP.Text = "HP: " + npcData.HitPoints;
                NpcAC.Text = "AC: " + npcData.Armour;
                NpcDR.Text = "DR: " + npcData.DamageResist;
                NpcExp.Text = "Exp: " + npcData.Experience * npcData.ExperienceMulti;
                NpcDrops.Text = "Drops " + GameResources.BuildDropList(npcData);
                NpcSpells.Text = "Spells Cast " + npcData.SpellMonsterCast;
            }

            this.MonsterToolTip.PlacementTarget = item;
            MonsterToolTip.Placement = PlacementMode.MousePoint;
            MonsterToolTip.IsOpen = true;

        }

        private void Hide_PopupMonsterToolTip(object sender, MouseEventArgs e)
        {
            MonsterToolTip.IsOpen = false;

        }

        private void Hide_ShopToolTip(object sender, MouseEventArgs e)
        {
           ShopToolTip.IsOpen = false;
        }

        private void Show_ShopToolTip(object sender, MouseEventArgs e)
        {

            ListViewItem item = e.Source as ListViewItem;

            UIUpdater.ShopEntry uItem = (UIUpdater.ShopEntry)item.Content;

            ResourceTypes.ItemType itemData = GameResources.FindItem(uItem.ShopItem);

            if (itemData != null)
            {
                ShopName.Text = "Name : " + itemData.Name;
                ShopArmourClass.Text = "AC : " + (itemData.ArmourClass / 10);
                ShopDamageResist.Text = "DR : " + (itemData.DamageResist / 10);
                ShopSpeed.Text = "Speed : " + itemData.Speed;
                ShopDamage.Text = "Damage : " + itemData.Min + " - " + itemData.Max;
                ShopAccuracy.Text = "Accuracy : " + itemData.Accuracy;
                ShopAbilityInfo.Text = GameResources.BuildAbilityList(itemData);
            }
            else
            {
                ShopArmourClass.Text = "No Data";
                ShopName.Text = "No Data";
                ShopArmourClass.Text = "No Data";
                ShopSpeed.Text = "No Data";
                ShopDamage.Text = "No Data";
                ShopAccuracy.Text = "No Data";
                ShopDamageResist.Text = "No Data";
                ShopAbilityInfo.Text = "No Data";
            }

            ShopToolTip.PlacementTarget = item;
            ShopToolTip.Placement = PlacementMode.MousePoint;
            ShopToolTip.IsOpen = true;
        }

        private void Show_PopupToolTip(object sender, MouseEventArgs e)
        {
            ListViewItem item = e.Source as ListViewItem;

            UIUpdater.ItemEntry uItem = (UIUpdater.ItemEntry)item.Content;

            ResourceTypes.ItemType itemData = GameResources.FindItem(uItem.ItemName);

            if (itemData != null)
            {
                Name.Text = "Name : " + itemData.Name;
                ArmourClass.Text = "AC : " + (itemData.ArmourClass/10);
                DamageResist.Text = "DR : " + (itemData.DamageResist / 10);
                Speed.Text = "Speed : " + itemData.Speed;
                Damage.Text = "Damage : " + itemData.Min + " - " + itemData.Max;
                Accuracy.Text = "Accuracy : " + itemData.Accuracy;
                AbilityInfo.Text = GameResources.BuildAbilityList(itemData);
            }
            else
            {
                ArmourClass.Text = "No Data";
                Name.Text = "No Data";
                ArmourClass.Text = "No Data";
                Speed.Text = "No Data";
                Damage.Text = "No Data";
                Accuracy.Text = "No Data";
                DamageResist.Text = "No Data";
                AbilityInfo.Text = "No Data";
            }

            MyToolTip.PlacementTarget = item;
            MyToolTip.Placement = PlacementMode.MousePoint;
            MyToolTip.IsOpen = true;
        }

        private void Hide_PopupToolTip(object sender, MouseEventArgs e)
        {
            MyToolTip.IsOpen = false;
        }
     
        private void inputCommand_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                CurrentCommand = inputCommand.Text + "\r\n";
                if (inputCommand.Text == "train stats")
                {

                }
                else
                {
                    gameDisplay.DispatchMessage(this, CurrentCommand);
                    inputCommand.Text = "";
                }
            }
            else if (e.Key >= Key.NumPad0 && e.Key <= Key.Decimal)
            {
                gameDisplay.DispatchMessage(this, GameCommandManager.ManualPlayerMovement(e));
                e.Handled = true;

            }
        }
    }
}
